﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Smart
{
    public partial class frmEvrYonetim : Form
    {
        public int Vade { get; set; }

        public frmEvrYonetim()
        {
            InitializeComponent();
        }

        private void btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmEvrYonetim_Load(object sender, EventArgs e)
        {
            EvrakYukle();
        }

        private void EvrakYukle()
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                using (Query q = new Query(ProgramTipleri.SqlCe))
                {
                    StringBuilder sb = new StringBuilder();
                    sb.Append("SELECT 'Sipariş' as [Evrak Cinsi],evr.evr_tarih AS Tarih, evr.evr_seri AS [Evrak Seri],evr.evr_sira AS [Evrak Sıra],cr.cari_adi1 AS [Cari Ünvanı1],cr.cari_adi2 AS [Cari Ünvanı2],evr.evr_cins, (case when evr.evr_gonderildi=0 then 'Gönderilmedi' else 'Gönderildi' end) as [Gonderildi] ");
                    sb.Append(" FROM         TBL_EVRAK as evr INNER JOIN TBL_CARI as cr ON evr.evr_carikod = cr.cari_kod  ");
                    sb.Append(" GROUP BY evr.evr_tarih, evr.evr_seri, evr.evr_sira, cr.cari_adi1, cr.cari_adi2,evr.evr_cins,evr.evr_gonderildi ");
                    sb.Append(" union all ");
                    sb.Append(" SELECT 'Tahsilat' as [Evrak Cinsi],ths.ths_tarih AS Tarih, ths.ths_seri AS [Evrak Seri],ths.ths_sira AS [Evrak Sıra],cri.cari_adi1 AS [Cari Ünvanı1],cri.cari_adi2 AS [Cari Ünvanı2],cast(0 as int) as ths_tip, (case when ths.ths_gonderildi=0 then 'Gönderilmedi' else 'Gönderildi' end) as [Gonderildi] ");
                    sb.Append(" FROM         TBL_TAHSILAT as ths INNER JOIN TBL_CARI as cri ON ths.ths_carikod = cri.cari_kod  ");
                    sb.Append(" GROUP BY ths.ths_tarih, ths.ths_seri, ths.ths_sira, cri.cari_adi1, cri.cari_adi2,ths.ths_gonderildi ");

                    q.Add(sb.ToString());
                    gridView2.VeriKaynagi = q.Fill();
                    gridView2.Columns[5].Width = -1;
                }
            }
            catch (Exception ex)
            {
                ExceptionHandling.Hand(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void EvrakSil()
        {
            using (Query q = new Query(ProgramTipleri.SqlCe))
            {
                string evrcins = gridView2[gridView2.CurrentRowIndex, 0].ToString();
                string seri = gridView2[gridView2.CurrentRowIndex, 2].ToString();
                int sira = Cevir.Parse<int>(gridView2[gridView2.CurrentRowIndex, 3]);
                int cins = Cevir.Parse<int>(gridView2[gridView2.CurrentRowIndex, 6]);
                string gonderildi = gridView2[gridView2.CurrentRowIndex, 7].ToString();
                if (gonderildi == "Gönderilmedi")
                {
                    if (evrcins == "Sipariş")
                    {
                        q.Add("delete from TBL_EVRAK where evr_seri=@seri and evr_sira=@sira and evr_cins=@cins");
                        q.Add("@cins", SqlDbType.NVarChar, cins);
                    }
                    else
                    {
                        q.Add("delete from TBL_TAHSILAT where ths_seri=@seri and ths_sira=@sira");
                    }
                    q.Add("@seri", SqlDbType.NVarChar, seri);
                    q.Add("@sira", SqlDbType.NVarChar, sira);

                    try
                    {
                        q.Exec();
                        gridView2.RemoveRow(gridView2.CurrentRowIndex);
                    }
                    catch (Exception ex)
                    {
                        ExceptionHandling.Hand(ex);
                    }
                }
                else
                {
                    MessageBox.Show("Gönderilmiş evrak silinemez !");
                }
            }
        }

        private void btnSil_Click(object sender, EventArgs e)
        {
            if (gridView2.CurrentRowIndex > -1)
            {
                EvrakSil();
            }
        }

        private void btnDegistir_Click(object sender, EventArgs e)
        {
            if (gridView2.CurrentRowIndex > -1)
            {
                string evrCins = gridView2[gridView2.CurrentRowIndex, 0].ToString();
                string seri = gridView2[gridView2.CurrentRowIndex, 2].ToString();
                int sira = Cevir.Parse<int>(gridView2[gridView2.CurrentRowIndex, 3]);
                int gonderildi = Cevir.Parse<int>(gridView2[gridView2.CurrentRowIndex, 7]);
                if (evrCins == "Sipariş")
                {
                    frmOfflineEvrak evr = new frmOfflineEvrak();
                    evr.EskiEvrak = true;
                    evr.EvrakSeri = seri;
                    evr.EvrakSira = sira;
                    evr.Vade = this.Vade;
                    if (gonderildi == 0) evr.Degistirilebilir = true; else evr.Degistirilebilir = false;
                    evr.Show();
                }
                else
                {
                    frmTahsilat ths = new frmTahsilat();
                    ths.EvrakSeri = seri;
                    ths.EvrakSira = sira;
                    ths.EskiEvrak = true;
                    if (gonderildi == 0) ths.Degistirilebilir = true; else ths.Degistirilebilir = false;
                    ths.Show();
                }
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (Query q = new Query(ProgramTipleri.SqlCe))
            {
                string evrcins = gridView2[gridView2.CurrentRowIndex, 0].ToString();
                string seri = gridView2[gridView2.CurrentRowIndex, 2].ToString();
                int sira = Cevir.Parse<int>(gridView2[gridView2.CurrentRowIndex, 3]);
                //int cins = Cevir.Parse<int>(gridView2[gridView2.CurrentRowIndex, 5]);
                //q.Add("UPDATE TBL_EVRAK SET evr_gonderildi=0 where evr_seri=@seri and evr_sira=@sira and evr_cins=@cins");
                if (evrcins == "Sipariş")
                {
                    q.Add("UPDATE TBL_EVRAK SET evr_gonderildi=0 where evr_seri=@seri and evr_sira=@sira ");
                }
                else
                {
                    q.Add("UPDATE TBL_TAHSILAT SET ths_gonderildi=0 where ths_seri=@seri and ths_sira=@sira ");
                }

                q.Add("@seri", SqlDbType.NVarChar, seri);
                q.Add("@sira", SqlDbType.NVarChar, sira);
                //q.Add("@cins", SqlDbType.NVarChar, cins);

                try
                {
                    q.Exec();
                    EvrakYukle();
                    MessageBox.Show("Tamamlandı !");
                }
                catch (Exception ex)
                {
                    ExceptionHandling.Hand(ex);
                }
            }
        }
    }
}